<?php
namespace modules\contact;
use \modules\auth\api as auth;
use \phiction\array_utils as arr;
use \phiction\exceptions\bad_request;

class api
{
    static function add($q, &$args)
    {
        $name      = arr::get_or_bad_request($args, 'name');
        $gender    = arr::get_or_bad_request($args, 'gender');
        $email     = arr::get_or_bad_request($args, 'email');
        $phone1    = arr::get_or_bad_request($args, 'phone1');
        $addr1     = arr::get_or_bad_request($args, 'addr1');
        $phone2    = arr::get_or($args, 'phone2', '');
        $addr2     = arr::get_or($args, 'addr2', '');
        $birthdate = new \DateTime(arr::get_or_bad_request($args, 'birthdate'));
        $comment   = arr::get_or($args, 'comment', '');

        $user = auth::validate_with_role($q, $args, 'salesman');
        $org  = $user['org'];

        if(storage::add($q, $name, $gender, $email, $phone1, $addr1,
                $phone2, $addr2, $birthdate, $comment, $org)->run() !== 1)
            throw new bad_request("添加联系人失败");
    }

    static function list_active($q, &$args)
    {
        $page = (int)arr::get_or_bad_request($args, 'page');

        $user = auth::validate_with_role($q, $args, 'salesman');
        $org  = $user['org'];

        $list = storage::list_by_active($q, $org, $page, true)->run();
        return $list;
    }


    static function list_archived($q, &$args)
    {
        $page = (int)arr::get_or_bad_request($args, 'page');

        $user = auth::validate_with_role($q, $args, 'salesman');
        $org  = $user['org'];

        $list = storage::list_by_active($q, $org, $page, false)->run();
        return $list;
    }

    static function contact_profile($q, &$args)
    {
        $id = (int)arr::get_or_bad_request($args, 'id');

        $user = auth::validate_with_role($q, $args, 'salesman');
        $org  = $user['org'];

        $profile = storage::profile($q, $org, $id)->run();
        if (is_null($profile))
            throw new bad_request("无此联系人");

        return $profile;
    }

    static function edit($q, &$args)
    {
        $id        = (int)arr::get_or_bad_request($args, 'id');
        $name      = arr::get_or_bad_request($args, 'name');
        $gender    = arr::get_or_bad_request($args, 'gender');
        $email     = arr::get_or_bad_request($args, 'email');
        $phone1    = arr::get_or_bad_request($args, 'phone1');
        $addr1     = arr::get_or_bad_request($args, 'addr1');
        $phone2    = arr::get_or($args, 'phone2', '');
        $addr2     = arr::get_or($args, 'addr2', '');
        $birthdate = new \DateTime(arr::get_or_bad_request($args, 'birthdate'));
        $comment   = arr::get_or($args, 'comment', '');

        $user = auth::validate_with_role($q, $args, 'salesman');
        $org = $user['org'];

        if(storage::edit($q, $id, $name, $gender, $email, $phone1, $addr1,
                $phone2, $addr2, $birthdate, $comment, $org)->run() !== 1)
            throw new bad_request("修改联系人资料失败");
    }

    static function archive($q, &$args)
    {
        $id = (int)arr::get_or_bad_request($args, 'id');

        $user = auth::validate_with_role($q, $args, 'salesman');
        $org  = $user['org'];

        if (storage::active($q, $org, $id, false)->run() !== 1)
            throw new bad_request("尘封联系人失败");
    }

    static function active($q, &$args)
    {
        $id = (int)arr::get_or_bad_request($args, 'id');

        $user = auth::validate_with_role($q, $args, 'salesman');
        $org  = $user['org'];

        if (storage::active($q, $org, $id, true)->run() !== 1)
            throw new bad_request("解封联系人失败");
    }

    static function list_by_contact($q, &$args)
    {
        $id = (int)arr::get_or_bad_request($args, 'id');

        $user = auth::validate_with_role($q, $args, 'salesman');
        $org  = $user['org'];

        if (storage::list_by_contact($q, $org, $id)->run() !== 1)
            throw new bad_request("查询失败");
    }
}

